//
//  FQFaceCerificationFailView.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/22.
//

import UIKit
import AttributedString

class FQFaceCerificationFailView: UIView {

    var closeBlock : (()->Void)?
    var pushBlock : (()->Void)?
    
    convenience init(_ type : FQUpdateType) {
        self.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 552))
        
        backgroundColor = .white
        clipsToBounds = true
        layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        layer.cornerRadius = 22
        
        let closeBtn = CloseButton(closeSize: CGSize(width: 12, height: 12), closeColor: .color153, lineWidth: 1)
        closeBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
        addSubview(closeBtn)
        
        closeBtn.snp.makeConstraints { make in
            make.right.equalTo(-20)
            make.top.equalTo(24)
        }
        
        let imageV = UIImageView(imageString: type == .face ? "icon_faceFail" : "icon_backFail")
        addSubview(imageV)
        
        imageV.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(55)
        }
        
        let titleLb = QMUILabel(font: .mSFont(19), textColor: .color51)
        addSubview(titleLb)
        
        titleLb.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(imageV.snp.bottom).offset(22)
        }
        
        let subTitleLb = QMUILabel(font: .rSFont(14), textColor: .color153)
        subTitleLb.text = "拍摄时请确保对准取景框，不要在偏暗环境下尝试"
        addSubview(subTitleLb)
        
        subTitleLb.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLb.snp.bottom).offset(22)
        }
        
        let tipView = FQCerificationTipView(1)
        addSubview(tipView)
        tipView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(subTitleLb.snp.bottom).offset(50)
            make.height.equalTo(tipView.qmui_height)
        }
        
        let str : ASAttributedString = """
        识别不通过？\("点击查看更多帮助>", .foreground(.colorMain), .action { [unowned self] in
            if self.pushBlock != nil {
                self.pushBlock!()
            }
        })
        """
        let tipLb = QMUILabel(font: .rSFont(14), textColor: .color51)
        tipLb.attributed.text = str
        
        tipLb.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-52)
        }
        
        let retryBtn = QMUIFillButton(fill: .colorMain, titleTextColor: .white)
        retryBtn.setTitle("重新识别", for: .normal)
        retryBtn.titleLabel?.font = .mSFont(19)
        retryBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
        addSubview(retryBtn)
        
        retryBtn.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(40)
            make.bottom.equalTo(tipLb.snp.top).offset(-17)
        }
    }

    @objc func close() {
        if closeBlock != nil {
            closeBlock!()
        }
    }
    
}
